package com.gyms.manager;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.okhttp.AppChangeUtils;
import com.gyms.R;

import utils.ScreenUtils;

/**
 * Title颜色管理类
 */
public class TitleManager {
    /**
     * 默认蓝色布局
     */
    private TitleTheme mTheme;//0是蓝色，1白色
    private View mTitlebar;//title布局
    private Context mContext;

    private ImageView mLeftView;// 左侧按钮
    public TextView mAfterBack;// 左侧紧贴后退文本
    private TextView mLeftTV;// 左侧文本
    private TextView mMidView;// 中间文本
    private ImageView mRightView;// 右侧按钮 右边只有一个显示Imageview
    private ImageView mRightViewR;// 有两个按钮显示，该按钮为右侧的按钮

    private TextView mRightTV;// 右侧文本
    private RelativeLayout mTitleBar;//标题栏
    private View lineNavigationBar;//线
    private ImageView statusBar;//状态栏

    public TitleManager(View mTitlebar, Context mContext, TitleTheme mTheme) {
        this.mTitlebar = mTitlebar;
        this.mContext = mContext;
        this.mTheme = mTheme;
        initView();
    }

    public ImageView getmRightView() {
        return mRightView;
    }

    public TitleTheme getmTheme() {
        return mTheme;
    }

    /**
     * 设置主题
     *
     * @param mTheme
     */
    public void setmTheme(TitleTheme mTheme) {
        this.mTheme = mTheme;
        setBarTitleColor();
    }

    /**
     * 初始化
     */
    private void initView() {
        mLeftView = (ImageView) mTitlebar.findViewById(R.id.iv_back);
        mAfterBack = (TextView) mTitlebar.findViewById(R.id.tv_after_back);
        mLeftTV = (TextView) mTitlebar.findViewById(R.id.tv_left);
        mMidView = (TextView) mTitlebar.findViewById(R.id.tv_title);
        mRightView = (ImageView) mTitlebar.findViewById(R.id.iv_img_right);
        mRightViewR = (ImageView) mTitlebar.findViewById(R.id.iv_img_right_right);
        mRightTV = (TextView) mTitlebar.findViewById(R.id.tv_right);
        mTitleBar = (RelativeLayout) mTitlebar.findViewById(R.id.NavigationBar);
        lineNavigationBar = mTitlebar.findViewById(R.id.lineNavigationBar);
        statusBar = (ImageView) mTitlebar.findViewById(R.id.statu_bar);
//            mRightView.setOnClickListener(this);
        setBarTitleColor();
    }

    private void setBarTitleColor() {
        ScreenUtils.translateStatusBar((Activity) mContext);

        switch (mTheme) {
            case Blue:
                if (null != statusBar) {

                    statusBar.setVisibility(View.VISIBLE);
                    ViewGroup.LayoutParams layoutParams = statusBar.getLayoutParams();
                    layoutParams.height = ScreenUtils.getStatusHeight(mContext);
                    statusBar.setLayoutParams(layoutParams);
                    statusBar.setBackgroundResource(R.color.colorPrimary);
                }
                break;
            case White:
                if (null != statusBar) {

                    statusBar.setVisibility(View.VISIBLE);
                    ViewGroup.LayoutParams layoutParams = statusBar.getLayoutParams();
                    layoutParams.height = ScreenUtils.getStatusHeight(mContext);
                    statusBar.setLayoutParams(layoutParams);
                    statusBar.setBackgroundResource(R.color.c_999999);
                }
                mTitleBar.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_FAFAFA));
                mLeftView.setImageResource(R.drawable.close_b_selector);
                mMidView.setTextColor(ContextCompat.getColor(mContext, R.color.c_333333));
                mRightTV.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                lineNavigationBar.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        if (mMidView != null) {
            mMidView.setText(title);
            mMidView.getPaint().setFakeBoldText(true);
            mMidView.getPaint().setAntiAlias(true);//抗锯齿
        }
    }

    /**
     * 右边的View
     * 默认隐藏
     *
     * @param buleId 资源id
     * @return
     */
    public ImageView setRightView(int... buleId) {
        mRightView.setVisibility(View.VISIBLE);
        for (int i = 0; i < buleId.length; i++) {
            if (i == mTheme.ordinal()) {
                mRightView.setImageResource(buleId[i]);
            }
        }
        return mRightView;
    }

    /**
     * 设置右侧资源
     * @param mResId 资源id
     * @return view
     */

    public ImageView setRightView(int mResId) {
        mRightView.setVisibility(View.VISIBLE);
        mRightView.setImageResource(mResId);
        return mRightView;
    }

    public void setRightViewOnclick(View.OnClickListener mClickListen){
        if(null==mClickListen)
            return;
        mRightView.setOnClickListener(mClickListen);
    }

    /**
     *右侧显示两个按钮的时候 设置最右侧的按钮样式
     * @param mResId 资源id
     * @return view
     */
    public ImageView setRightViewR(int mResId) {
        if(mResId==R.mipmap.icon_fenxiang||mResId==R.mipmap.icon_fenxiang_w){//场馆通不允许分享
            mRightViewR.setVisibility(AppChangeUtils.isGymApp?View.VISIBLE:View.GONE);
        }else {
            mRightViewR.setVisibility(View.VISIBLE);
        }

        mRightViewR.setImageResource(mResId);
        return mRightViewR;
    }

    /**
     * 获取中间的TitleView
     */
    public TextView getMidView() {
        return mMidView;
    }

    /**
     * 设置左边的TextView
     *
     * @param leftText
     * @return
     */
    public TextView setLeftText(String leftText) {
        mLeftTV.setText(leftText);
        mLeftTV.setVisibility(View.VISIBLE);
        return mLeftTV;
    }

    /**
     * 设置右边的TextView
     *
     * @param rightText
     * @return
     */
    public TextView setRightText(String rightText) {
        mRightTV.setText(rightText);
        mRightTV.setVisibility(View.VISIBLE);
        return mRightTV;
    }

    /**
     * 设置左边的关闭按钮的图片
     *
     * @param
     * @return
     */
    public TextView setLeftCloseImg(int res) {
        mLeftView.setImageResource(res);
        mLeftView.setVisibility(View.VISIBLE);
        return mRightTV;
    }

    /**
     * 得到右边按钮文字
     *
     * @return
     */
    public TextView getmRightTV() {
        return mRightTV;
    }

    /**
     * 返回键
     */
    public View setBack() {
        if (mLeftView != null) {
            mLeftView.setVisibility(View.VISIBLE);
        }
        return mLeftView;
    }

    public enum TitleTheme {
        Blue, White
    }

    public ImageView getmLeftView() {
        return mLeftView;
    }
}
